package edu.fiu.cis.nursing.server.data;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import au.com.bytecode.opencsv.CSVReader;
import edu.fiu.cis.nursing.client.NursingService;
import edu.fiu.cis.nursing.shared.DefaultBlock;
import edu.fiu.cis.nursing.shared.ManagedBlock;
import edu.fiu.cis.nursing.shared.NursingRecord;
import edu.fiu.cis.nursing.shared.Query;
import edu.fiu.cis.nursing.shared.UserToken;

public class DataProvider implements NursingService {

	private static final String CODE = "Patient Code";
	private static final String NAME = "Name";

	private Map<Integer, String> attrToIdMap = new HashMap<Integer, String>();
	private List<Map<String, String>> _data = new ArrayList<Map<String, String>>();

	public DataProvider() {
		ManagedBlock.exceptionHandled(new DefaultBlock() {

			@Override
			public Void execute() throws Exception {
				CSVReader reader = new CSVReader(new FileReader("data/uhc.csv"));
				List<String[]> data = reader.readAll();

				int c = 0;
				for (String a : data.get(0))
					attrToIdMap.put(c++, a.trim());

				for (int i = 1; i < 6; i++) {
					c = 0;
					Map<String, String> m = new HashMap<String, String>();
					_data.add(m);
					for (String v : data.get(i)) {
						String a = attrToIdMap.get(c++);
						m.put(a, v);
					}
				}
				return null;
			}
		});

	}

	@Override
	public ArrayList<NursingRecord> search(Query query)
			throws IllegalArgumentException {

		ArrayList<NursingRecord> list = new ArrayList<NursingRecord>();

		for (Map<String, String> m : _data) {
			String pc = m.get(CODE);
			String name = m.get(NAME);

			boolean isIn = true;
			if (query.getCode() != null
					&& !query.getCode().equalsIgnoreCase(pc)) {
				isIn = false;
			}

			if (query.getName() != null
					&& !name.toLowerCase().contains(
							query.getName().toLowerCase())) {
				isIn = false;
			}

			if (isIn) {
				list.add(createRecord(m));
			}

		}

		return list;
	}

	@Override
	public NursingRecord load(String code) {

		for (Map<String, String> m : _data) {
			if (m.get(CODE).equals(code)) {
				return createRecord(m);
			}
		}
		return null;
	}

	private NursingRecord createRecord(Map<String, String> m) {
		if (m == null) {
			return null;
		}
		return new NursingRecord(m);
	}
	
	public static void main(String[] args) {
		DataProvider impl = new DataProvider();
		for (String a : impl.attrToIdMap.values()){
			StringBuffer sb = new StringBuffer(a);
			sb.append('\t');
			for (Map<String,String> m : impl._data){
				sb.append(m.get(a)).append('\t');
			}
			System.out.println(sb.toString());
		}
	}

	@Override
	public UserToken validateCookie(String cookie) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UserToken loginUser(String userId, String password) {
		// TODO Auto-generated method stub
		return null;
	}

}
